#include "mainwindow.h"
#include "ui_mainwindow.h"

QHash<QString,QChart::ChartTheme> tmpChartTheme = {
    {"ChartThemeBrownSand",QChart::ChartThemeBrownSand},
    {"ChartThemeLight",QChart::ChartThemeLight},
    {"ChartThemeBlueCerulean",QChart::ChartThemeBlueCerulean},
    {"ChartThemeBlueIcy",QChart::ChartThemeBlueIcy},
    {"ChartThemeBlueNcs",QChart::ChartThemeBlueNcs},
    {"ChartThemeDark",QChart::ChartThemeDark},
    {"ChartThemeHighContrast",QChart::ChartThemeHighContrast},
    {"ChartThemeQt",QChart::ChartThemeQt}
};

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    // 设置窗口标题
    setWindowTitle("Qt Charts Demo");

    // 创建一个中心部件来容纳布局
    QWidget *centralWidget = new QWidget(this);
    setCentralWidget(centralWidget);

    // 创建一个垂直布局
    QVBoxLayout *mainLayout = new QVBoxLayout(centralWidget);

    mainLayout->addWidget(createComboBox());

    // 创建并添加饼状图到布局中
    mainLayout->addWidget(MainWindow::createPieChart());

    // 创建并添加柱状图到布局中
    mainLayout->addWidget(MainWindow::createBarChart());
}

MainWindow::~MainWindow()
{
    delete ui;
}

QChartView* MainWindow::createPieChart()
{
    // 1. 创建 QPieSeries
    QPieSeries *pieSeries = new QPieSeries();
    pieSeries->append("苹果 (40)", 40);
    pieSeries->append("香蕉 (20)", 20);
    pieSeries->append("橙子 (30)", 30);
    pieSeries->append("葡萄 (10)", 10);

    // 2. 为每个切片设置标签可见性，并设置字体，slice(切片)
    for (QPieSlice *slice : pieSeries->slices())
    {
        slice->setLabelVisible(true);
        // 可以为每个切片设置不同的颜色
        if (slice->label() == "苹果 (40)")
        {
            slice->setBrush(QColor("#FF4C4C")); // 红色
        }
        else if (slice->label() == "香蕉 (20)")
        {
            slice->setBrush(QColor("#FFDE4C")); // 黄色
        }
        else if (slice->label() == "橙子 (30)")
        {
            slice->setBrush(QColor("#FF9F4C")); // 橙色
        }
        else
        {
            slice->setBrush(QColor("#774CFF")); // 紫色
        }
    }

    // 3. 创建 QChart
    m_pieChartChart = new QChart();

    //添加系列，Serise(系列)，legend(图例)，setAlignment(设置对齐方式)，AlignRight(右对齐)
    m_pieChartChart->addSeries(pieSeries);
    m_pieChartChart->setTitle("水果消费比例");
    m_pieChartChart->legend()->setAlignment(Qt::AlignRight); // 设置图例位置为右对齐

    // 4. 创建 QChartView
    QChartView *chartView = new QChartView(m_pieChartChart);
    chartView->setRenderHint(QPainter::Antialiasing); // 开启抗锯齿
    return chartView;
}

QChartView* MainWindow::createBarChart()
{
    // 1. 创建 QBarSet，用来存放每组数据
    QBarSet *set1 = new QBarSet("小明");
    *set1 << 5 << 10 << 8 << 12 << 7;

    QBarSet *set2 = new QBarSet("小红");
    *set2 << 7 << 12 << 9 << 8 << 15;

    // 2. 创建 QBarSeries，将 QBarSet 添加到其中
    QBarSeries *barSeries = new QBarSeries();
    barSeries->append(set1);
    barSeries->append(set2);

    // 3. 创建 QChart
    m_barChart = new QChart();
    m_barChart->addSeries(barSeries);
    m_barChart->setTitle("每月销售额对比");
    m_barChart->legend()->setVisible(true);
    m_barChart->legend()->setAlignment(Qt::AlignBottom);

    // 4. 定义 x 轴类别
    QStringList categories;
    categories << "1月" << "2月" << "3月" << "4月" << "5月";
    QBarCategoryAxis *axisX = new QBarCategoryAxis();
    axisX->append(categories);
    m_barChart->addAxis(axisX, Qt::AlignBottom);
    barSeries->attachAxis(axisX);

    // 5. 定义 y 轴（数值轴）
    QValueAxis *axisY = new QValueAxis();
    axisY->setRange(0, 20);
    axisY->setTitleText("销售额 (万)");
    m_barChart->addAxis(axisY, Qt::AlignLeft);
    barSeries->attachAxis(axisY);

    // 6. 创建 QChartView
    QChartView *chartView = new QChartView(m_barChart);
    chartView->setRenderHint(QPainter::Antialiasing); // 开启抗锯齿
    return chartView;
}

QComboBox *MainWindow::createComboBox()
{
    QComboBox *comboBox = new QComboBox;

    // 添加列表项
    comboBox->addItem("ChartThemeBrownSand", tmpChartTheme["ChartThemeBrownSand"]); // 文本和关联数据
    comboBox->addItem("ChartThemeLight", tmpChartTheme["ChartThemeLight"]);
    comboBox->addItem("ChartThemeBlueCerulean", tmpChartTheme["ChartThemeBlueCerulean"]);
    comboBox->addItem("ChartThemeBlueIcy", tmpChartTheme["ChartThemeBlueIcy"]);
    comboBox->addItem("ChartThemeBlueNcs", tmpChartTheme["ChartThemeBlueNcs"]);
    comboBox->addItem("ChartThemeDark", tmpChartTheme["ChartThemeDark"]);
    comboBox->addItem("ChartThemeHighContrast", tmpChartTheme["ChartThemeHighContrast"]);
    comboBox->addItem("ChartThemeQt", tmpChartTheme["ChartThemeQt"]);

    //QOverload是用来方式信号函数重载的
    connect(comboBox, QOverload<int>::of(&QComboBox::currentIndexChanged),this,
                     [=](int index){
                         QString text = comboBox->itemText(index);
                         QVariant data = comboBox->itemData(index);
                         qDebug() << "Current index changed to:" << index;
                         qDebug() << "Selected data ID:" << data.toInt();
                         m_pieChartChart->setTheme(static_cast<QChart::ChartTheme>(data.toInt()));
                        m_barChart->setTheme(static_cast<QChart::ChartTheme>(data.toInt()));
                     });

    return comboBox;
}
